package com.example.transaction.service.impl;

import com.example.transaction.model.TransactionRecord;
import com.example.transaction.repository.TransactionRecordRepository;
import com.example.transaction.service.TransactionRecordService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@Service
@RequiredArgsConstructor
@Slf4j
public class TransactionRecordServiceImpl implements TransactionRecordService {

    private final TransactionRecordRepository transactionRecordRepository;

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public TransactionRecord createTransactionRecord(String fromAccount, String toAccount, BigDecimal amount) {
        String transactionId = UUID.randomUUID().toString();
        log.info("Creating transaction record: {}, from: {}, to: {}, amount: {}", 
                transactionId, fromAccount, toAccount, amount);
        
        TransactionRecord record = new TransactionRecord(
                transactionId,
                fromAccount,
                toAccount,
                amount,
                TransactionRecord.TransactionStatus.PENDING
        );
        
        return transactionRecordRepository.save(record);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public TransactionRecord updateTransactionStatus(String transactionId, TransactionRecord.TransactionStatus status) {
        log.info("Updating transaction status: {}, status: {}", transactionId, status);
        
        TransactionRecord record = transactionRecordRepository.findByTransactionId(transactionId)
                .orElseThrow(() -> new RuntimeException("Transaction record not found: " + transactionId));
        
        record.setStatus(status);
        return transactionRecordRepository.save(record);
    }

    @Override
    @Transactional(readOnly = true)
    public TransactionRecord getTransactionRecord(String transactionId) {
        log.info("Getting transaction record: {}", transactionId);
        return transactionRecordRepository.findByTransactionId(transactionId)
                .orElseThrow(() -> new RuntimeException("Transaction record not found: " + transactionId));
    }

    @Override
    @Transactional(readOnly = true)
    public List<TransactionRecord> getAccountTransactions(String accountNumber) {
        log.info("Getting transactions for account: {}", accountNumber);
        return transactionRecordRepository.findByFromAccountOrToAccount(accountNumber, accountNumber);
    }

    @Override
    @Transactional(readOnly = true)
    public List<TransactionRecord> getAllTransactionRecords() {
        log.info("Getting all transaction records");
        return transactionRecordRepository.findAll();
    }
}